﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TELOS_Konverter
{
    class StringHandler
    {
        MessageHandler mh = new MessageHandler();

        /**isIn
         * @param: - list of strings that shall be checked
         *         - element that will be checked against the list
         * @return: true if element in list, false otherwise
         */
        public bool isIn(List<string> flags, string el)
        {
            for (int i = 0; i < flags.Count; i++)
            {
                if (flags[i].Equals(el))
                    return true;
            }
            return false;
        }

        /**delete
         * @param: - original string s
         *         - search string "search"
         *         - list of strings, flags, that determine what to do with the original string
         *         --> before vs. after && include vs. exclude
         * @return: string with deleted parts, depending on the flags
         */
        public string delete(string s, string search, List<string> flags)
        {
            string cache = "";
            if (isIn(flags, "before"))
            {
                if (isIn(flags, "include"))
                {
                    try
                    {
                        for (int x = s.IndexOf(search) + search.Length; x < s.Length; x++)
                            cache = cache + s[x];
                    }
                    catch (Exception e)
                    {
                        //mh.alert(cache);
                    }
                }
                else if (isIn(flags, "exclude"))
                {
                    for (int x = s.IndexOf(search); x < s.Length; x++)
                    {
                        cache = cache + s[x];
                    }
                }
            }
            else if (isIn(flags, "after"))
            {
                if (isIn(flags, "include"))
                {
                    for (int x = 0; x < s.IndexOf(search); x++)
                    {
                        cache = cache + s[x];
                    }
                }
                else if (isIn(flags, "exclude"))
                {
                    for (int x = 0; x < s.IndexOf(search) + search.Length; x++)
                    {
                        cache = cache + s[x];
                    }
                }
            }
            return cache;
        }

        /**isStringInString
         * @param: - string
         *         - substring
         * @return: true if substring is in string, else otherwise     
         */
        public bool isStringInString(String s, String subs)
        {
            if (s.IndexOf(subs) >= 0)
                return true;
            else
                return false;
        }

        public bool isStringInString(string s, string subs, bool lowercase)
        {
            if (lowercase)
            {
                subs = subs.ToLower();
                s = s.ToLower();
            }

            return isStringInString(s, subs);
        }
    }
}
